package com.lzs.adx.web.commmon.utils;

import lombok.extern.slf4j.Slf4j;
import org.apache.http.NameValuePair;
import org.apache.http.client.utils.URIBuilder;

import java.io.UnsupportedEncodingException;
import java.net.URI;
import java.net.URISyntaxException;
import java.net.URLDecoder;
import java.net.URLEncoder;
import java.nio.charset.StandardCharsets;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Slf4j
public class URLUtils {

    public static String removeParameter(String url, String parameterName) {
        try {
            URIBuilder uriBuilder = new URIBuilder(url);
            List<NameValuePair> filteredParams = uriBuilder.getQueryParams().stream()
                    .filter(param -> !param.getName().equals(parameterName))
                    .collect(Collectors.toList());
            uriBuilder.clearParameters();
            uriBuilder.addParameters(filteredParams);
            return uriBuilder.build().toString();
        }catch (Exception exception){
            log.error("地址处理错误",exception);
        }
        return null;
    }


    public static String removeEmpTyParameter(String url) {
        try {
            URIBuilder uriBuilder = new URIBuilder(url);
            List<NameValuePair> filteredParams = uriBuilder.getQueryParams().stream()
                    .filter(param -> !param.getValue().startsWith("__"))
                    .filter(param -> !param.getValue().startsWith("_"))
                    .filter(param -> !param.getValue().startsWith("{"))
                    .collect(Collectors.toList());
            uriBuilder.clearParameters();
            uriBuilder.addParameters(filteredParams);
            return uriBuilder.build().toString();
        }catch (Exception exception){
            log.error("地址处理错误",exception);
        }
        return null;
    }




    public static void main(String[] args) {
            String url = "http://www.baidu.com?a=1&b=2&c=__IP__&d=_E_&e={f}";
            String result = removeEmpTyParameter(url);
            System.out.println(result); // 输出: http://www.baidu.com?b=2

    }
}